from django.shortcuts import render

# Create your views here.
from django.http import JsonResponse
from rest_framework.decorators import api_view
from driver_school_after.utils import is_blank,get_token
from django.core.paginator import Paginator
from login.models import Student_teach,Student_train,Student,Coach_info,Car_info,Subject
#------------------------训练记录-------预约训练-------学员表----教练表-----车辆信息--科目

#查询训练记录
@api_view(['get'])
def select_student_teach(request):
    token = request.headers.get('token')
    page = request.GET.get('page')
    pagesize = request.GET.get('pagesize')
    if get_token(token) is None:
        return JsonResponse({'status': 204, 'message': '请先登录'})

    user_token = get_token(token)
    print(user_token)
    name = user_token['name']
    stuobj = Student_teach.objects.filter(stu__name=name).order_by('-train_time')
    total = stuobj.count()
    if page is None or int(page) < 1:
        pagesize = 1
    if pagesize is None or int(pagesize)>total:
        pagesize = total
    stupa = Paginator(stuobj,pagesize)
    objlist = stupa.page(page)
    list = []
    for i in objlist:
        list.append({
            'record_id':i.record_id,
            'ground':i.ground,
            'train_time':i.train_time,
            'comment_student':i.comment_student,
            'comment_coach':i.comment_coach,
            'stu':{
                'id':i.stu.id,
                'name':i.stu.name,
                'age':i.stu.age,
                'sex':i.stu.sex,
                'phone':i.stu.phone,
                'create_time':i.stu.create_time,
                'is_graduate':i.stu.is_graduate,
                'end_time':i.stu.end_time,
                'image':i.stu.image
            },
            'coa':{
                'coach_id':i.coa.coach_id,
                'coach_name':i.coa.coach_name,
                'coach_phone':i.coa.coach_phone,
                'image':i.coa.image

            },
            'car':{
                'car_id':i.car.car_id,
                'car_pai':i.car.car_pai,
                'car_model':i.car.car_model,
                'car_state':i.car.car_state,
                'car_notes':i.car.car_notes,
                'image':i.car.image
            },
            'test':{
                'subject_id':i.test.subject_id,
                'subject_name':i.test.subject_name,
                'subject_detail':i.test.subject_detail

            }
        })
    return JsonResponse({'status':200,'message':'查询成功','data':list,'total':total})
